package com.zhou.JavaAndroidFirm.http;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author Administrator
 * @date 2018/7/16 0016
 * @des
 */
public class RetrofitClient {
    private OkHttpClient mOkHttpClient;
    private String mBaseUrl;
    private Retrofit mRetrofit;

    public RetrofitClient(OkHttpClient mOkHttpClient, String mBaseUrl) {
        this.mOkHttpClient = mOkHttpClient;
        this.mBaseUrl = mBaseUrl;
    }


    public RetrofitClient setBaseUrl(String baseUrl) {
        this.mBaseUrl = baseUrl;
        return this;
    }


    public <T> T builder(Class<T> apiService) {
        if (this.mBaseUrl == null) {
            throw new RuntimeException("基础url不能为空");
        }
        if (apiService == null) {
            throw new RuntimeException("构造接口类不能为空");
        }
        mRetrofit = new Retrofit.Builder()
                .client(mOkHttpClient)
                .baseUrl(mBaseUrl)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return mRetrofit.create(apiService);
    }
}
